﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GonguklubburinnMVC.Models;

namespace GonguklubburinnMVC.Controllers
{
	[HandleError]
	public class HomeController : Controller
	{
		MountainRepository repo = new MountainRepository();

		public ActionResult Index()
		{
			var model = repo.ShowAllWithYes();
			return View(model);
		}

		public ActionResult IndexNO()
		{
			var model = repo.ShowAllWithNO();
			return View(model);
		}

		public ActionResult Details(int id)
		{
			MountainE m = repo.GetMountain(id);
			if (m == null)
				return View("NotFound");
			return View(m);
		}

		public ActionResult About()
		{
			return View();
		}
		[Authorize(Roles = "Admin")]
		public ActionResult Edit(int id)
		{
			MountainE mountain = repo.GetMountain(id);
			return View(mountain);

		}

		//
		// POST: /Mountain/Edit/5

		[HttpPost]
		[Authorize(Roles = "Admin")]
		public ActionResult Edit(int id, FormCollection collection)
		{

			var mntain2Update = repo.GetMountain(id);
			// Add validation Rules to mntain2Update 

			TryUpdateModel(mntain2Update, new String[] { "Name", "Height", "Hasgone", "ImgName", "InsertDate", "TravelTime", "HasGone" }, collection.ToValueProvider());
			if (ModelState.IsValid)
			{
				repo.save();
				return RedirectToAction("Index");

			}
			// Otherwise, reshow form
			return View(mntain2Update);

		}
	}
}
